package com.own.business.exam.controller;


import com.own.business.exam.business.service.ExamPaperService;
import com.own.business.exam.common.exception.ExamException;
import com.own.business.exam.entity.vo.ExamPaperVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.common.util.list.ListUtil;
import com.own.component.store.core.confirm.aop.ConfirmAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ExamPaperController
 *
 * @author chenxueli
 * @date 2021-06-18 10:21:00
 */
@Slf4j
@Tag(name = "考试试卷" + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "/exam/paper")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ExamPaperController extends BaseController {

    @Resource
    private ExamPaperService examPaperService;

    @Operation(summary = "根据考试id获取试卷信息")
    @GetMapping("/exam/{examId}")
    public ResultModel<ExamPaperVo> getPaperByExamId(
            @Parameter(description = "考试id", required = true) @PathVariable Long examId
    ) {
        var loginUser = SessionUserUtil.getLoginUser();
        var vo = examPaperService.getPaperByExamId(loginUser.userId(), loginUser.userId() + "", examId);
        return R.success(vo);
    }

    @Operation(summary = "根据考试id获取试卷的分数信息")
    @GetMapping("/score/{examId}")
    public ResultModel<BigDecimal> getScoreByExamId(
            @Parameter(description = "考试id", required = true) @PathVariable Long examId
    ) {
        var loginUser = SessionUserUtil.getLoginUser();
        var score = examPaperService.getScoreByExamId(loginUser.userId(), examId);
        return R.success(score);
    }

    @Operation(summary = "根据考试id获取试卷信息（不会生成试卷）")
    @GetMapping("/paper/{examId}")
    public ResultModel<ExamPaperVo> getResultPaperByExamId(
            @Parameter(description = "考试id", required = true) @PathVariable Long examId
    ) {
        var loginUser = SessionUserUtil.getLoginUser();
        var vo = examPaperService.getResultPaperByExamId(loginUser.userId(), examId);
        return R.success(vo);
    }

    @Operation(summary = "根据考试ids获取试卷信息")
    @GetMapping("/result/{examIds}")
    public ResultModel<List<ExamPaperVo>> getResultByExamIdList(
            @Parameter(description = "考试ids", required = true) @PathVariable String examIds
    ) {
        var examIdList = ListUtil.Spilt.toLong(examIds, ",");
        if (examIdList.isEmpty()) {
            throw new ExamException("考试信息不能为空");
        }
        var loginUser = SessionUserUtil.getLoginUser();
        var paperList = examIdList.stream().map(examId -> {
            var paper = examPaperService.getResultPaperByExamId(loginUser.userId(), examId);
            if (paper != null) {
                var rank = examPaperService.getRankByExamId(loginUser.userId(), examId);
                paper.setRank(rank);
            }
            return paper;
        }).collect(Collectors.toList());
        return R.success(paperList);
    }

    @ConfirmAop(module = "exam", message = "确认交卷？", key = "submit_exam_confirm_key", enforce = true)
    @Operation(summary = "确认提交试卷")
    @PostMapping("/submit/{examId}")
    public ResultModel<ExamPaperVo> checkPractice(
            @Parameter(description = "考试id", required = true) @PathVariable Long examId
    ) {
        var loginUser = SessionUserUtil.getLoginUser();
        var vo = examPaperService.submit(loginUser.userId(), examId);
        return R.success(vo);
    }

}
